﻿#include "DoubaoApi.h"

DoubaoAI::DoubaoAI() {}

/**
 * @brief 向豆包AI发送请求并获取垃圾分类结果
 * @param question 用户提问
 * @return AI返回的原始垃圾分类结果
 */
QString DoubaoAI::DoubaoAI_request(QString& question)
{
    // 优化提问方式，明确要求只返回四种分类之一
    QString optimizedQuestion = question ;

    QNetworkRequest request;
    QNetworkAccessManager manager;
    request.setUrl(QUrl(OPENAI_BASE_URL));

    //头部信息设置
    QString token = "Bearer " + OPENAI_API_KEY;
    request.setRawHeader("Authorization", token.toUtf8());
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");

    //post附加信息,用json格式构建请求体内容
    QJsonArray context;
    QJsonObject textobj;
    textobj["type"] = "text";
    textobj["text"] = optimizedQuestion;  // 使用优化后的问题
    context.append(textobj);

    QJsonArray messagesarray;
    QJsonObject messageobj;
    messageobj["role"] = "user";
    messageobj["content"] = context;
    messagesarray.append(messageobj);

    QJsonObject data;
    data["model"] = OPENAI_MODEL;
    data["messages"] = messagesarray;

    QJsonDocument doc(data);
    QByteArray postData = doc.toJson();

    QSslConfiguration config = QSslConfiguration::defaultConfiguration();
    config.setProtocol(QSsl::AnyProtocol);
    config.setPeerVerifyMode(QSslSocket::VerifyNone);
    request.setSslConfiguration(config);

    //发送post请求
    QNetworkReply *reply = manager.post(request, postData);

    //循环等待服务器返回数据
    QEventLoop waitserver;
    connect(reply, &QNetworkReply::finished, &waitserver, &QEventLoop::quit);
    waitserver.exec();

    if(reply != nullptr && reply->error() == QNetworkReply::NoError){
        //得到返回的数据
        QByteArray reply_data = reply->readAll();
        //对返回的json数据进行解析
        QJsonObject obj = QJsonDocument::fromJson(reply_data).object();
        QJsonArray choicesarray = obj.value("choices").toArray();
        if (!choicesarray.isEmpty()){
            QJsonObject choiceobj = choicesarray[0].toObject();
            if (choiceobj.contains("message") && choiceobj["message"].isObject()) {
                QJsonObject messageobj = choiceobj["message"].toObject();
                if (messageobj.contains("content") && messageobj["content"].isString()) {
                    // 直接返回AI的原始响应内容
                    return messageobj["content"].toString();
                }
            }
        }
    } else {
        qDebug() << "https request error:" << reply->errorString();
    }
}

